package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.*;
import com.wing.cabinet.model.request.StorageInventoryInoutDetailForm;
import com.wing.cabinet.model.request.StorageInventoryInoutForm;
import com.wing.cabinet.model.request.UseInventoryInoutDetailForm;
import com.wing.cabinet.model.request.UseInventoryInoutForm;
import com.wing.cabinet.model.response.*;
import com.wing.cabinet.service.*;
import com.wing.common.constant.CabAuthCode;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.Result;
import com.wing.order.model.request.OrderReceiveLoadForm;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "App-暂存柜存刀/取刀;存/取维修/回收刀明细管理", tags = "暂存柜存刀/取刀;存/取维修/回收刀明细管理")
@RestController("appStorageInventoryInoutDetailController")
@RequestMapping("cabinet/app/storageInventoryInoutDetail")
@Slf4j
public class StorageInventoryInoutDetailController {

    @Autowired
    private StorageInventoryInoutDetailService storageInventoryInoutDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private StorageInventorySpaceService storageInventorySpaceService;

    @Autowired
    private StorageInventoryInoutService storageInventoryInoutService;

    @Autowired
    private StorageInventoryProductService productService;

    @Autowired
    private CabinetCargoSpaceService cabinetCargoSpaceService;

    @Autowired
    private StorageInventoryKnifeService storageInventoryKnifeService;


    @ApiOperation(value = "查询所有暂存柜存刀/取刀;存/取维修/回收刀明细", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<StorageInventoryInoutDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long inoutId
    ){

        Page<StorageInventoryInoutDetail> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("inoutId",inoutId);
        IPage<StorageInventoryInoutDetailVO> list = storageInventoryInoutDetailService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("detail/{id}")
    public JsonResult detail(@PathVariable("id") Long storageInventoryInoutDetailId) {
        StorageInventoryInoutDetailVO storageInventoryInoutDetailVO = storageInventoryInoutDetailService.get4appById(storageInventoryInoutDetailId);
        if (storageInventoryInoutDetailVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), storageInventoryInoutDetailVO);
    }

    @PostMapping()
    public JsonResult add(@Validated StorageInventoryInoutDetailForm storageInventoryInoutDetailForm) {
        if (storageInventoryInoutDetailService.add(storageInventoryInoutDetailForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "存暂存刀具", notes = "")
    @PostMapping("/storage/inventoryIn")
    public JsonResult inventoryStorageIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("storage param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutForm.getStorageType())
                    &&!cabAuthCodes.contains(CabAuthCode.TEMP_STORAGE_AUTH_CODE)){
                return JsonResult.fail("此用户无暂存权限,请配置此用户角色的暂存权限");
            }
//            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
//            useInventoryInoutForm.getDetailList().get(0).setProcessId(useInventoryInoutForm.getProcessId());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            storageInventoryInoutDetailService.inventoryIn(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.temp,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.tempIn,useInventoryInoutForm.getRemark());
            return JsonResult.success();
        } catch (Exception e) {
            log.error("storage inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "取暂存刀具", notes = "")
    @PostMapping("/storage/inventoryOut")
    public JsonResult inventoryStorageOut(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("inventory out param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.PICK_KNIFE_AUTH_CODE)){
                return JsonResult.fail("此用户无取刀权限,请配置此用户角色的取刀权限");
            }
            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
            useInventoryInoutForm.getDetailList().get(0).setProcessId(useInventoryInoutForm.getProcessId());
            storageInventoryInoutDetailService.inventoryOut(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.temp,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.tempOut,UseInventoryInout.BusinessType.tempOut);
            return JsonResult.success();
        }catch (BusinessException e1){
            log.error("storage inventory out fail", e1);
            return JsonResult.fail(Result.Code.REQUIRE_REFRESH.getCode(),e1.getMessage());
        }catch (Exception e) {
            log.error("storage inventory out fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "取上新刀具", notes = "")
    @PostMapping("/new/inventoryOut")
    public JsonResult inventoryStorageNewOut(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("inventory out param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.RECEIVE_KNIFE_AUTH_CODE)){
                return JsonResult.fail("此用户无取刀权限,请配置此用户角色的取刀权限");
            }
            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
            useInventoryInoutForm.getDetailList().get(0).setProcessId(useInventoryInoutForm.getProcessId());
            StorageInventoryInout storageInventoryInout=storageInventoryInoutDetailService.inventoryOut(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.exhibit,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.newOut,UseInventoryInout.BusinessType.newOut);
            return JsonResult.success(storageInventoryInout);
        }catch (BusinessException e1){
            log.error("storage inventory out fail", e1);
            return JsonResult.fail(Result.Code.REQUIRE_REFRESH.getCode(),e1.getMessage());
        }catch (Exception e) {
            log.error("storage inventory out fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存刀柜清空", notes = "")
    @PostMapping("/empty/inventoryOut")
    public JsonResult inventoryStorageOutEmpty(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("inventory out empty param:"+ JSONObject.toJSONString(useInventoryInoutForm));
//            String cabAuthCodes= JwtUtils.getCabAuthCodes();
//            if (!cabAuthCodes.contains(CabAuthCode.RECEIVE_KNIFE_AUTH_CODE)){
//                return JsonResult.fail("此用户无取刀权限,请配置此用户角色的取刀权限");
//            }
            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
            useInventoryInoutForm.getDetailList().get(0).setProcessId(useInventoryInoutForm.getProcessId());
            storageInventoryInoutDetailService.inventoryOutEmpty(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.exhibit,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.newOut,UseInventoryInout.BusinessType.newOut);
            return JsonResult.success();
        }catch (BusinessException e1){
            log.error("storage inventory out fail", e1);
            return JsonResult.fail(Result.Code.REQUIRE_REFRESH.getCode(),e1.getMessage());
        }catch (Exception e) {
            log.error("storage inventory out fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存维修刀具", notes = "")
    @PostMapping("/repair/inventoryIn")
    public JsonResult inventoryRepairIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("storage repair param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.REPAIR_STORAGE_AUTH_CODE)){
                return JsonResult.fail("此用户无报修权限,请配置此用户角色的报修权限");
            }
//            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            storageInventoryInoutDetailService.inventoryIn(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.repair,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.repairIn,useInventoryInoutForm.getRemark());
            return JsonResult.success();
        } catch (Exception e) {
            log.error("repair inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "小柜子存维修刀具", notes = "")
    @PostMapping("/repair/xgzInventoryIn")
    public JsonResult xgzInventoryIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("【小柜子】storage repair param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.REPAIR_STORAGE_AUTH_CODE)){
                return JsonResult.fail("此用户无报修权限,请配置此用户角色的报修权限");
            }
//            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            storageInventoryInoutDetailService.xgzInventoryRepairIn(useInventoryInoutForm, StorageInventoryInout.BusinessType.repair,StorageInventoryInout.BusinessDetailType.repairIn);
            return JsonResult.success();
        } catch (Exception e) {
            log.error("【小柜子】repair inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存回收/报废刀具", notes = "")
    @PostMapping("/recycle/inventoryIn")
    public JsonResult inventoryRecycleIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("storage recycle param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.RECYCLE_STORAGE_AUTH_CODE)){
                return JsonResult.fail("此用户无报废权限,请配置此用户角色的报废权限");
            }
//            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
//            useInventoryInoutForm.getDetailList().get(0).setRemark(useInventoryInoutForm.getRemark());
//            useInventoryInoutForm.getDetailList().get(0).setCategoryId(useInventoryInoutForm.getCategoryId());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            storageInventoryInoutDetailService.inventoryIn(useInventoryInoutForm.getDetailList(), StorageInventoryInout.BusinessType.recycle,useInventoryInoutForm.getDeviceCode(),StorageInventoryInout.BusinessDetailType.recycleIn,useInventoryInoutForm.getRemark());
            return JsonResult.success();
        } catch (Exception e) {
            log.error("recycle inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "【小柜子】存回收/报废刀具", notes = "")
    @PostMapping("/recycle/xgzInventoryIn")
    public JsonResult xgzInventoryRecycleIn(@RequestBody UseInventoryInoutForm useInventoryInoutForm) {
        try {
            log.info("【小柜子】storage recycle param:"+ JSONObject.toJSONString(useInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.RECYCLE_STORAGE_AUTH_CODE)){
                return JsonResult.fail("此用户无报废权限,请配置此用户角色的报废权限");
            }
//            useInventoryInoutForm.getDetailList().get(0).setWorkOrderId(useInventoryInoutForm.getWorkOrderId());
//            useInventoryInoutForm.getDetailList().get(0).setRemark(useInventoryInoutForm.getRemark());
//            useInventoryInoutForm.getDetailList().get(0).setCategoryId(useInventoryInoutForm.getCategoryId());
//            useInventoryInoutForm.getDetailList().get(0).setUseTime(useInventoryInoutForm.getUseTime());
            storageInventoryInoutDetailService.xgzInventoryRecycleIn(useInventoryInoutForm, StorageInventoryInout.BusinessType.recycle,StorageInventoryInout.BusinessDetailType.recycleIn);
            return JsonResult.success();
        } catch (Exception e) {
            log.error("【小柜子】recycle inventory in fail", e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "获取暂存库位", notes = "")
    @PostMapping("/storage/getSpacesOld")
    public JsonResult getStorageSpacesOld(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.temp);
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的公共库位");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }

            }
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取暂存库位", notes = "")
    @PostMapping("/storage/getSpaces")
    public JsonResult getStorageSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCodes(), StorageInventoryInout.BusinessType.temp,useInventoryInoutDetailForm.getQuantity());
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    //return JsonResult.fail("未找到可用的公共库位");
                    return JsonResult.fail("已超过最大容量，建议分批次存入");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }

            }
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取盘验暂存库位", notes = "")
    @PostMapping("/storage/getCheckSpaces")
    public JsonResult getStorageCheckSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getCheckSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            Map params=new HashMap();
            params.put("cabCodes",useInventoryInoutDetailForm.getCabCode().split(","));
            params.put("spaceCode4Detail",useInventoryInoutDetailForm.getSpaceCode());
            List<StorageInventoryKnifeVO> inventorySpaces= storageInventoryKnifeService.list4app(params);
            if (inventorySpaces.size()==1){
                List<CabinetCargoSpace> cabinetCargoSpaces=cabinetCargoSpaceService.list(new QueryWrapper<CabinetCargoSpace>().in("cab_code",useInventoryInoutDetailForm.getCabCode().split(",")).eq("code",useInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()>0&&(
                        (CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType()))||
                                (CabinetCargoSpace.SpaceType.temp.equals(cabinetCargoSpaces.get(0).getSpaceType()))
                )){
                    CabinetCargoSpaceVO spaceVO=new CabinetCargoSpaceVO();
                    spaceVO.setCabCode(inventorySpaces.get(0).getCabCode());
                    spaceVO.setSpaceCode(inventorySpaces.get(0).getSpaceCode());
                    spaceVO.setCode(inventorySpaces.get(0).getSpaceCode());
                    return JsonResult.success(spaceVO);
                }

            }
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.temp);
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的公共库位");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }

            }
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取专属暂存库位", notes = "")
    @PostMapping("/storage/getUserSpacesV1")
    public JsonResult getStorageUserSpacesV1(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getUserSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoSpace.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoSpace.SpaceType.temp.getValue());
            CabSpaceResultVO spaceVO = storageInventoryInoutDetailService.getUserSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.temp,spaceTypes);
            return JsonResult.success(spaceVO.getList());
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取专属暂存库位", notes = "")
    @PostMapping("/storage/getUserSpacesV2")
    public JsonResult getStorageUserSpacesV2(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getUserSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoSpace.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoSpace.SpaceType.temp.getValue());
            CabSpaceResultVO spaceVO = storageInventoryInoutDetailService.getUserSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.temp,spaceTypes);
            return JsonResult.success(spaceVO.getList());
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取专属暂存库位", notes = "")
    @PostMapping("/storage/getUserSpaces")
    public JsonResult getStorageUserSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getUserSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoSpace.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoSpace.SpaceType.temp.getValue());
            CabSpaceResultVO cabSpaceResultVO = storageInventoryInoutDetailService.getUserSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCodes(), StorageInventoryInout.BusinessType.temp,spaceTypes,useInventoryInoutDetailForm.getQuantity());
            List<CabinetCargoSpaceVO> spaceVOList=cabSpaceResultVO.getList();
            if (spaceVOList.size()==0){
                if (cabSpaceResultVO.getIsHasUserSpace()){
                    return JsonResult.fail("当前用户分配的专属库位已经存满，请联系管理员新增专属格子",new ArrayList<>());
                }else{
                    return JsonResult.fail("暂未配置当前用户的专属库位，请联系管理员新增专属格子",new ArrayList<>());
                }

            }
            return JsonResult.success(spaceVOList);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage(),new ArrayList<>());
        }

    }

    @ApiOperation(value = "获取维修库位", notes = "")
    @PostMapping("/repair/getSpacesOld")
    public JsonResult getRepairSpacesOld(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.repair);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.repair);
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的公共库位");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }
            }
        }catch (Exception e){
            log.error("get repair space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取维修库位", notes = "")
    @PostMapping("/repair/getSpaces")
    public JsonResult getRepairSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.repair);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCodes(), StorageInventoryInout.BusinessType.repair,useInventoryInoutDetailForm.getQuantity());
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    //return JsonResult.fail("未找到可用的公共库位");
                    return JsonResult.fail("已超过最大容量，建议分批次存入");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }
            }
        }catch (Exception e){
            log.error("get repair space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取回收/报废库位", notes = "")
    @PostMapping("/recycle/getSpacesOld")
    public JsonResult<CabinetCargoSpaceVO> getRecycleSpacesOld(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.recycle);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), StorageInventoryInout.BusinessType.recycle);
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的公共库位");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }
            }
        }catch (Exception e){
            log.error("get recycle space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取回收/报废库位", notes = "")
    @PostMapping("/recycle/getSpaces")
    public JsonResult<CabinetCargoSpaceVO> getRecycleSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("getSpaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.recycle);
            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getStorageType(),useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCodes(), StorageInventoryInout.BusinessType.recycle,useInventoryInoutDetailForm.getQuantity());
            if (spaceVO != null) {
                return JsonResult.success(spaceVO);
            } else {
                if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专属库位,请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
                    return JsonResult.fail("未找到可用的专类库位，请先进行库位设置");
                }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
                    //return JsonResult.fail("未找到可用的公共库位");
                    return JsonResult.fail("已超过最大容量，建议分批次存入");
                }else{
                    return JsonResult.fail("存贮性质参数错误");
                }
            }
        }catch (Exception e){
            log.error("get recycle space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }


    @ApiOperation(value = "打开维修库位", notes = "")
    @PostMapping("/repair/getCheckedSpaces")
    public JsonResult getRepairCheckedSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            return storageInventoryInoutDetailService.getCheckedSpace(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.repair);
        }catch (Exception e){
            log.error("openRecycleRepairSpaces fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "打开回收/报废库位", notes = "")
    @PostMapping("/recycle/getCheckedSpaces")
    public JsonResult getRecycleCheckedSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            return storageInventoryInoutDetailService.getCheckedSpace(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.recycle);
        }catch (Exception e){
            log.error("openRecycleRepairSpaces fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "打开回收/报废库位", notes = "")
    @PostMapping("/repair/getUsedSpaces")
    public JsonResult getUsedRepairSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            CabinetCargoSpaceVO spaceVO= storageInventoryInoutDetailService.getUsedSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.repair);
            return JsonResult.success(spaceVO);
        }catch (Exception e){
            log.error("getUsedSpaces fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "打开回收/报废库位", notes = "")
    @PostMapping("/recycle/getUsedSpaces")
    public JsonResult getUsedRecycleSpaces(@RequestBody UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            CabinetCargoSpaceVO spaceVO= storageInventoryInoutDetailService.getUsedSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.recycle);
            return JsonResult.success(spaceVO);
        }catch (Exception e){
            log.error("getUsedSpaces fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "打开维修库位", notes = "")
    @PostMapping("/getCheckedSpaces")
    public JsonResult getCheckedSpace(UseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            return storageInventoryInoutDetailService.getCheckedSpace(useInventoryInoutDetailForm, null);
        }catch (Exception e){
            log.error("openRecycleRepairSpaces fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

//    @ApiOperation(value = "盘验", notes = "盘验")
//    @GetMapping("/check")
    public JsonResult check(Long spaceId, StorageInventoryInout.BusinessType businessType){
        try{
//            storageInventoryInoutDetailService.check(spaceId,businessType);
            return JsonResult.success();
        }catch (Exception e){
            log.error("check fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "盘验", notes = "盘验")
    @PostMapping("/check")
    public JsonResult check(@RequestBody StorageInventoryInoutForm storageInventoryInoutForm){
        try{
            log.info("check param:{}",JSONObject.toJSONString(storageInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.CHECK_AUTH_CODE)){
                return JsonResult.fail("此用户无盘验权限,请配置此用户角色的盘验权限");
            }
            storageInventoryInoutDetailService.check4Wx(storageInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("check fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

//    @ApiOperation(value = "盘验（柜子面板）", notes = "盘验")
//    @PostMapping("/check4Cab")
    public JsonResult check4Cab(@RequestBody StorageInventoryInoutDetailForm storageInventoryInoutDetailForm){
        try{
            storageInventoryInoutDetailService.check(storageInventoryInoutDetailForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("check fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }



    @ApiOperation(value = "盘验（柜子面板）", notes = "盘验")
    @PostMapping("/check4Cab")
    public JsonResult check4Cab(@RequestBody StorageInventoryInoutForm storageInventoryInoutForm){
        try{
            log.info("check params:{}",JSON.toJSONString(storageInventoryInoutForm));
            String cabAuthCodes= JwtUtils.getCabAuthCodes();
            if (!cabAuthCodes.contains(CabAuthCode.CHECK_AUTH_CODE)){
                return JsonResult.fail("此用户无盘验权限,请配置此用户角色的盘验权限");
            }
            storageInventoryInoutDetailService.check(storageInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("check fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "维修订单签收", notes = "签收")
    @GetMapping("/sign")
    public JsonResult sign(Long orderId, BigDecimal repairPrice, Integer quantitySign){
        try{
            storageInventoryInoutDetailService.sign(orderId,repairPrice,quantitySign);
            return JsonResult.success();
        }catch (Exception e){
            log.error("sign fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "通知回收/维修", notes = "通知")
    @GetMapping("/notify")
    public JsonResult notify(Long id,Long supplierId,String supplierName){
        try{
            storageInventoryInoutDetailService.notify(id,supplierId,supplierName);
            return JsonResult.success();
        }catch (Exception e){
            log.error("sign fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }
    /**
     * 我的待办任务列表    TODO 未搜索
     */
    @ApiOperation(value = "待办任务", notes = "查询")
    @GetMapping("/toDoTask")
    public PageResult<StorageInventoryInoutDetailVO> selTask(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order
    ) {
        Page<StorageInventoryInoutDetailVO> pg = new Page<>(page, limit);

        Page<StorageInventoryInoutDetailVO> list = storageInventoryInoutDetailService.toDoTask(pg);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    /**
     * 我的待办任务  报废申请 TODO 未确定数据
     *
     * @param id 报废单/修磨单id
     */
    @ApiOperation(value = "待办任务 报废申请", notes = "查询")
    @GetMapping("/selTaskPro")
    public JsonResult selTaskPro(@RequestParam(name = "skuCode") String id) {


        List<ScrapApplyVO> list = storageInventoryInoutDetailService.selTaskPro(id);

        return JsonResult.success(list);
    }


    /**
     * 盘验
     * TODO 数量未确定
     *
     * @param spaceCode    货道编号
     * @param skuCode      产品编号
     * @param businessType 业务状态
     */
    @Transactional
    @GetMapping("/openContainer")
    public JsonResult openContainer(@RequestParam String spaceCode, @RequestParam String skuCode, @RequestParam String businessType) {

        //查询货道的产品库存
        QueryWrapper<StorageInventorySpace> spaceQueryWrapper = new QueryWrapper<>();
        spaceQueryWrapper.eq("space_code", spaceCode);
        spaceQueryWrapper.eq("sku_code", skuCode);
        StorageInventorySpace space = storageInventorySpaceService.getOne(spaceQueryWrapper);

        //查询暂存柜产品库存
        QueryWrapper<StorageInventoryProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("space_code", spaceCode);
        productQueryWrapper.eq("sku_code", skuCode);
        StorageInventoryProduct product = productService.getOne(productQueryWrapper);

        //查询 暂存柜存刀/取刀;存/取维修/回收刀明细
        QueryWrapper<StorageInventoryInoutDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("space_code", spaceCode);
        queryWrapper1.eq("sku_code", skuCode);
        StorageInventoryInoutDetail detail = storageInventoryInoutDetailService.getOne(queryWrapper1);

        //查询 暂存柜存刀/取刀;存/取维修/回收刀
        QueryWrapper<StorageInventoryInout> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", detail.getInoutId());
        StorageInventoryInout inout = storageInventoryInoutService.getOne(queryWrapper2);

        //暂存
        if ("temp".equals(businessType) && StringUtils.isNotBlank(businessType)) {

            //设置id 修改暂存表状态
            inout.setId(detail.getInoutId());
            inout.setBusinessType(StorageInventoryInout.BusinessType.temp);
            inout.setStatus(StorageInventoryInout.Status.checked);
            storageInventoryInoutService.updateById(inout);

            //修改暂存明细表状态
            detail.setBusinessType(StorageInventoryInoutDetail.BusinessType.temp);
            storageInventoryInoutDetailService.updateById(detail);


            //减少库存 修改库存产品的状态
            product.setBusinessType(StorageInventoryProduct.BusinessType.temp);
            product.setQuantity(product.getQuantity() - detail.getQuantity());
            productService.updateById(product);

            space.setBusinessType(StorageInventorySpace.BusinessType.temp);
            space.setQuantity(space.getQuantity() - detail.getQuantity());
            storageInventorySpaceService.updateById(space);
        }
        //维修
        if ("repair".equals(businessType) && StringUtils.isNotBlank(businessType)) {

            //设置id 修改暂存表状态
            inout.setId(detail.getInoutId());
            inout.setBusinessType(StorageInventoryInout.BusinessType.repair);
            inout.setStatus(StorageInventoryInout.Status.checked);
            storageInventoryInoutService.updateById(inout);
            //修改暂存明细表状态
            detail.setBusinessType(StorageInventoryInoutDetail.BusinessType.repair);
            storageInventoryInoutDetailService.updateById(detail);

            //减少库存 修改库存产品的状态
            product.setBusinessType(StorageInventoryProduct.BusinessType.repair);
            product.setQuantity(product.getQuantity() - detail.getQuantity());
            productService.updateById(product);

            space.setBusinessType(StorageInventorySpace.BusinessType.repair);
            space.setQuantity(space.getQuantity() - detail.getQuantity());
            storageInventorySpaceService.updateById(space);
        }
        //回收
        if ("recycle".equals(businessType) && StringUtils.isNotBlank(businessType)) {
            //设置id 修改暂存表状态
            inout.setId(detail.getInoutId());
            inout.setBusinessType(StorageInventoryInout.BusinessType.recycle);
            inout.setStatus(StorageInventoryInout.Status.checked);
            storageInventoryInoutService.updateById(inout);
            //修改暂存明细表状态
            detail.setBusinessType(StorageInventoryInoutDetail.BusinessType.recycle);
            storageInventoryInoutDetailService.updateById(detail);

            //减少库存 修改库存产品的状态
            product.setBusinessType(StorageInventoryProduct.BusinessType.recycle);
            product.setQuantity(product.getQuantity() - detail.getQuantity());
            productService.updateById(product);

            space.setBusinessType(StorageInventorySpace.BusinessType.recycle);
            space.setQuantity(space.getQuantity() - detail.getQuantity());
            storageInventorySpaceService.updateById(space);
        }
        return JsonResult.success("以盘验通过");
    }

    /**
     * TODO 未搜索  数据未完善
     * 盘验任务列表
     */
    @GetMapping("/diskTask")
    public PageResult<ScrapApplyVO> diskTask(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order
    ) {

        Page<ScrapApplyVO> pg = new Page<>(page, limit);

        Page<ScrapApplyVO> list = storageInventoryInoutDetailService.diskTask(pg);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 盘验 根据柜子编号查询当前柜子的信息
     *
     * @param cabCode 刀柜编号
     */
    @GetMapping("/getCabinet")
    public JsonResult getCabinet(@RequestParam String cabCode) {
        ScrapApplyVO cabinet = storageInventoryInoutDetailService.getCabinet(cabCode);
        return JsonResult.success(cabinet);
    }

    /**
     * 盘验 柜子下库位的信息 -app
     *
     * @param cabCode 刀柜编号
     * @screen screen 筛选  业务类型
     */
    @GetMapping("/diskStorage")
    public PageResult<ScrapApplyVO> diskStorage(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order
            , @RequestParam(name = "cabCode", defaultValue = "") String cabCode
            , @RequestParam(name = "screen", required = false) String screen
    ) {
        Page<ScrapApplyVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCode)) {
            params.put("cabCode", cabCode);
        }

        //筛选
        if (StringUtils.isNotBlank(screen)) {

            int i = Integer.parseInt(screen);
            params.put("screen", i);
        }

        Page<ScrapApplyVO> list = storageInventoryInoutDetailService.diskStorage(params, pg);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    /**
     * 盘验结果 异常/正常
     */
    @GetMapping("/diskResultException")
    public PageResult<ScrapApplyVO> diskResult(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order) {

        Page<ScrapApplyVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        Page<ScrapApplyVO> list = storageInventoryInoutDetailService.diskResult(pg, params);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 盘验结果 异常/正常
     */
    @GetMapping("/diskResultNormal")
    public PageResult<ScrapApplyVO> diskResultNormal(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order) {

        Page<ScrapApplyVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        Page<ScrapApplyVO> list = storageInventoryInoutDetailService.diskResultNormal(pg, params);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    /**
     * 点击通知回收 TODO
     */


    /**
     * 维修订单 维修中 列表-app
     * TODO 数据未对
     * repair 维修中
     * finsh 已完成
     */
    @GetMapping("/repairOrder")
    public JsonResult repairOrder(@RequestParam String shared) {

        //维修中
        if ("repair".equals(shared)){
             List<ScrapApplyVO> list = storageInventoryInoutDetailService.repairOrder();
             return JsonResult.success(list);
        }else {
         //以完成
            Page<StorageInventoryInoutDetailVO> page = storageInventoryInoutDetailService.toDoTask(new Page(1, 10));
            return JsonResult.success(page.getRecords());
        }
    }

    /**
     * 维修订单 维修中 确认签收
     * TODO 数据未对
     * TODO 维修价格未写
     * @param id 订单编号
     */
    @GetMapping("/repairSign")
    @Transactional
    public JsonResult repairSign(@RequestParam Integer id) {
        //获取暂存柜明细
        StorageInventoryInoutDetail inoutDetail = storageInventoryInoutDetailService.getById(id);
        //获取暂存柜
        StorageInventoryInout inout = storageInventoryInoutService.getById(inoutDetail.getInoutId());

        //将维修状态改为暂存(已完成)
        inoutDetail.setBusinessType(StorageInventoryInoutDetail.BusinessType.temp);
        if (!storageInventoryInoutDetailService.updateById(inoutDetail)) {
            return JsonResult.fail("修改失败");
        }
        //将维修状态改为暂存(已完成)
        inout.setBusinessType(StorageInventoryInout.BusinessType.temp);
        if (!storageInventoryInoutService.updateById(inout)) {
            return JsonResult.fail("修改失败");
        }
        return JsonResult.success("已完成");
    }

    @ApiOperation(value = "格子柜上货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/loadSimple")
    public JsonResult latticeCabinetUp(@Validated(OrderReceiveLoadForm.Add.class) @RequestBody OrderReceiveLoadForm orderReceiveLoadForm) {
        try{
//            String cabAuthCodes= JwtUtils.getCabAuthCodes();
//            if (!cabAuthCodes.contains(CabAuthCode.LOAD_AUTH_CODE)){
//                return JsonResult.fail("此用户无上货权限,请配置此用户角色的上货权限");
//            }
            storageInventoryInoutDetailService.latticeCabinetUp(orderReceiveLoadForm);
            return JsonResult.success("上货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "格子柜下货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/cellUnload")
    public JsonResult cellUnload(@Validated(OrderReceiveLoadForm.Add.class) @RequestBody OrderReceiveLoadForm orderReceiveLoadForm) {
        try{
//            String cabAuthCodes= JwtUtils.getCabAuthCodes();
//            if (!cabAuthCodes.contains(CabAuthCode.UNLOAD_AUTH_CODE)){
//                return JsonResult.fail("此用户无下货权限,请配置此用户角色的下货权限");
//            }
            storageInventoryInoutDetailService.latticeCabinetUnload(orderReceiveLoadForm);
            return JsonResult.success("下货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

}
